/**
 * 
 */
package info.niwota.webshot;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebBackForwardList;
import android.webkit.WebHistoryItem;
import android.webkit.WebView;

/**
 * @author qiangli
 * 
 */
public final class History {
	public static final String SCHEME = "history";
	private static final String TAG = "History";

	private History() {
	}

	private static String format(WebBackForwardList items) {
		final StringBuilder sb = new StringBuilder();
		sb.append("<html><body>");
		try {
			final int idx = (items == null ? -1 : items.getCurrentIndex());
			if (idx > 0) {
				final String prefix = SCHEME + "://localhost/";
				appendLink(sb, "&laquo;Back", prefix + "?steps=-1");
				sb.append("<br />");
				//
				sb.append("<ul>");
				if (Debug.DEBUG) {
					Log.d(TAG, "format index: " + idx + " size: "
							+ items.getSize());
				}
				for (int i = 0; i < idx; i++) {
					WebHistoryItem item = items.getItemAtIndex(i);
					int steps = (i - idx);
					appendItem(sb, item.getTitle(), prefix + "?steps=" + steps
							+ "&url=" + item.getUrl());
				}
				sb.append("</ul>");
			}
		} catch (Exception e) {
			sb.append(e.toString());
		}
		sb.append("</body></html>");
		//
		final String s = sb.toString();
		if (Debug.DEBUG) {
			Log.d(TAG, "format: " + s);
		}
		return s;
	}

	private static void appendLink(StringBuilder sb, String title, String url) {
		sb.append("<a href=\"");
		sb.append(url);
		sb.append("\">");
		sb.append(title);
		sb.append("</a><br />");
	}

	private static void appendItem(StringBuilder sb, String title, String url) {
		sb.append("<li><a href=\"");
		sb.append(url);
		sb.append("\">");
		sb.append(title);
		sb.append("</a></li><br />");
	}

	public static void load(WebView webview, WebBackForwardList l) {
		// webview != null
		try {
			String data = format(l);
			webview.loadDataWithBaseURL("file:///android_asset/", data,
					"text/html", "UTF-8", null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static String nvl(String s1, String s2) {
		if (TextUtils.isEmpty(s1)) {
			return s2;
		}
		return s1;
	}

	public static void showHistoryBackDialog(final Context ctx,
			final WebView webview, WebBackForwardList l) {
		final AlertDialog.Builder b = new AlertDialog.Builder(ctx);
		b.setCustomTitle(null);
		b.setTitle(null);
		final int idx = (l == null ? -1 : l.getCurrentIndex());
		final int size = l.getSize();
		if (Debug.DEBUG) {
			Log.d(TAG, "showHistoryBackDialog size: " + size +" idx: " + idx);
		}
		if (idx > 0) {
			final CharSequence[] cs = new CharSequence[idx];
			for (int i = 0; i < idx; i++) {
				WebHistoryItem item = l.getItemAtIndex(i);
				String t = nvl(item.getTitle(), item.getUrl());
				cs[i] = nvl(t, "");
				if (Debug.DEBUG) {
					Log.d(TAG, "showHistoryBackDialog cs[ " + i +" ] " + cs[i] +" id: " + item.getId() +" url: " + item.getUrl() +" original: " + item.getOriginalUrl() +" title: " + item.getTitle());
				}
			}
			b.setItems(cs, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					int steps = (which - idx);
					webview.goBackOrForward(steps);
				}
			});
			final AlertDialog d = b.create();
			d.show();
		}
	}
	
	public static void showHistoryForwardDialog(final Context ctx,
			final WebView webview, WebBackForwardList l) {
		final AlertDialog.Builder b = new AlertDialog.Builder(ctx);
		b.setCustomTitle(null);
		b.setTitle(null);
		final int idx = (l == null ? -1 : l.getCurrentIndex());
		final int size = l.getSize();
		if (idx >= 0 && size > 0) {
			final CharSequence[] cs = new CharSequence[idx];
			for (int i = idx + 1; i < size; i++) {
				WebHistoryItem item = l.getItemAtIndex(i);
				String t = nvl(item.getTitle(), item.getUrl());
				t = nvl(t, "");
				cs[i - idx - 1] = t;
			}
			b.setItems(cs, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					int steps = (which + 1);
					webview.goBackOrForward(steps);
				}
			});
		}
		final AlertDialog d = b.create();
		d.show();
	}
}
